<template>
    <div>
        <div class="title">
            <van-icon name="arrow-left" @click="$router.go(-1)"/>
            <span>设置</span>
        </div>
        <div class="userInfo">
            <img :src="userInfo.avatarUrl">
            <div class="nickName">{{ userInfo.nickName }}</div>
        </div>
        <div class="cell">
            <div class="item" @click="toPage('resetting')">修改个人信息</div>
            <div class="item">更改密码</div>
        </div>
        <div class="btn">
            <van-button type="warning" @click="logout">退出登录</van-button>
        </div>
    </div>
</template>

<script>
    export default {
        name: "Setting",
        data() {
            return {
                userInfo: this.$store.state.userInfo
            }
        },
        methods: {
            logout() {
                this.$post('auth/logout')
                .then(res => {
                    console.log(res)
                    // 退出登录清除 vuex 相对应的数据
                    this.$store.dispatch('setToken')
                    this.$store.dispatch('setUserInfo')
                    this.$store.dispatch('setCollect')
                    // 清除 缓存里面的数据
                    localStorage.removeItem('userInfo')
                    localStorage.removeItem('collect')
                    // 路由跳转
                    this.$router.push({name: 'user'})
                })
            },
            toPage(name) {
                this.$router.push({name: name})
            },
        }
    }
</script>

<style lang="less" scoped>
  .title {
    position: relative;
    width: 375px;
    height: 45px;
    line-height: 45px;
    font-size: 18px;

    >span {
      margin-left: 50px;
    }
    .van-icon-arrow-left {
      position: absolute;
      left: 0px;
      line-height: 45px;
      margin-left: 11px;
      font-size: 18px;
    }
  }

  .userInfo {
    display: flex;
    flex-direction: row;
    padding-left: 21px;
    padding-bottom: 5px;
    line-height: 80px;
    img {
      width: 80px;
      height: 80px;
      border-radius: 50%;
    }
    .nickName {
      margin-left: 14px;
    }
  }

  .cell {
    margin-top: 21px;
    margin-left: 21px;
    margin-right: 21px;

    .item {
      padding-left: 14px;
      margin-bottom: 14px;
      height: 40px;
      line-height: 40px;
      font-size: 16px;
      border-radius: 10px;
      background-color: #fff;
    }
  }

  .btn {
    text-align: center;

    .van-button__content {
      width: 180px;
    }
  }
</style>
